<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class LibArray {
	var $_elements = array();
	
	//constructor
	function LibArray($elements = '') {
		if( is_array($elements) ) {
			$this->_elements = $elements;
		}
	}
	
	//methods
	function push($element) {
		return array_push($this->_elements, $element);
	}
	
	function add($element) {
		return ($this->push($element) > 0 ? true : false);
	}
	
	function adds($elements) {
		foreach($elements as $k => $element) {
			$this->add($element);
		}
	}
	
	function pop() {
		return array_pop($this->_elements);
	}
	
	function first() {
		return $this->_elements['0'];
	}
	
	function last() {
		return end($this->_elements);
	}
	
	function clear() {
		$this->_elements = array();
	}
	
	function set($key, $element) {
		$_element = $this->get($key);
		$this->_elements[$key] = $element;
		return $_element;
	}
	
	function put($key, $element) {
        $_element = $this->get($key);
        $this->_elements[$key] = &$element;
        return $_element;
	}
	
	function get($key) {
		return $this->_elements[$key];
	}
	
	function gets($ks) {
		$elements = array();
		foreach($ks as $key) {
			$elements[] = $this->_elements[$key];
		}
		return $elements;
	}
	
	function range($offset, $length = 1) {
		return array_slice($this->_elements, $offset, $length);
	}
	
	function delete($key) {
		unset($this->_elements[$key]);
		reset($this->_elements);
	}
	
	function deletes($ks) {
		foreach($ks as $key) {
			unset($this->_elements[$key]);
		}
		reset($this->_elements);
	}
	
	function remove($key) {
		$_element = $this->get($key);
		if( !is_null($_element) ) {
			array_splice($this->_elements, $key, 1);
		}
		return $_element;
	}
	
	function removes($offset, $length = 1) {
		array_splice($this->_elements, $offset, $length);
	}
	
	//
	function contain($element) {
		return (array_search($element, $this->_elements) !== false);
	}
	
	function containKey($key) {
		return array_key_exists($key, array_keys($this->_elements));
	}
	
	function containValue($value) {
		return in_array($value, array_values($this->_elements));
	}
	
	//type
	//	1	only
	//	2	one and more
	//	3	all
	function contains($elements, $type = '1') {
		return (array_search($elements, $this->_elements) !== false);
	}
	
	//
	function reset() {
		reset($this->_elements);
	}
	
	function unique() {
		$this->_elements = array_unique($this->_elements);
	}
	
	function reverse($preserve = false) {
		$this->_elements = array_reverse($this->_elements, $preserve);
	}
	
	function shift() {
		return array_shift($this->_elements);
	}
	
	function unshift() {
		$c = func_num_args();
		$v = func_get_args();
		for($i=0; $i<$c; $i++) {
			$this->_elements = array_unshift($this->_elements, $v[$i]);
		}
	}
	
	function indexOf($element) {
		return array_search($element, $this->_elements);
	}
	
	function lastIndexOf($element) {
		$_elements = array_reverse($this->_elements);
	}
	
	function isEmpty() {
		return empty($this->_elements);
	}
	
	function isUnique() {
		return (count($this->_elements) == count(array_unique($this->_elements)));
	}
	
	function size() {
		return count($this->_elements);
	}
	
	function position($element) {
		return array_search($element, $this->_elements);
	}
	
	function pos() {
		return $this->position($this->current());
	}
	
	function current() {
		return current($this->_elements);
	}
	
	function next() {
		return next($this->_elements);
	}
	
	function prev() {
		return prev($this->_elements);
	}
	
	function toArray() {
		return $this->_elements;
	}
	
	function keys() {
		return array_keys($this->_elements);
	}
	
	function values() {
		return array_values($this->_elements);
	}
	
	//
	function sort($type = 'sort') {
		$types = array(
				'sort', 'rsort',
				'ksort', 'krsort',
				'asort', 'arsort',
				'natsort', 
				'reset',
					);
		if( in_array($type, $types) ) {
			$type($this->_elements);
		}
	}
	
	function doaction($action = 'sort') {
		$actions = array('sort');
		if( in_array($action, $actions) ) {
			$action($this->_elements);
		}
	}
	
	function p($m) {
		$ms = array('a');
		if( in_array($action, $actions) ) {
			$action($this->_elements);
		}
	}
	
	function gp($m) {
		
	}
}
//
?>